package com.skcc.ts.nsi.expense;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.MEDataHandler;
import com.skcc.ts.nsi.service.SocketClientService;

public class MEExpenseListResult extends Activity {
	private String myId;
	private String fromD;
	private String toD;
	private String pcd;
	private String acd;
	private String[] expTime;
	private String[] expNumber;
	private String[] expPjtName;
	private String[] expAcntCode;
	private String[] expAcntName;
	private String[] expMoney;
	private String[] expSerial;
	private String addr;
	private String port;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meexpense_list_result);

		getList();// 정산목록정보 수신

		TableLayout numberTable = (TableLayout) findViewById(R.id.expenseTable);
		TableLayout numberTable2 = (TableLayout) findViewById(R.id.expenseTable);
		TableLayout numberTable3 = (TableLayout) findViewById(R.id.expenseTable);

		TableRow tableR = null;
		TableRow tableR2 = null;
		TableRow tableR3 = null;

		for (int i = 0; i < expTime.length; i++) {
			tableR = new TableRow(this);
			tableR.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			tableR2 = new TableRow(this);
			tableR2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			tableR3 = new TableRow(this);
			tableR3.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			final TextView text_date_and_money_list = new TextView(this);
			final TextView text_pjt_list = new TextView(this);
			final TextView text_acnt_list = new TextView(this);

			final TextView text_date = new TextView(this);
			final TextView text_pjt = new TextView(this);
			final TextView text_acnt = new TextView(this);
			final TextView text_money = new TextView(this);
			final String acntCode;

			if (i % 2 == 1) {
				tableR.setBackgroundColor(0xffd3daeb);
				tableR2.setBackgroundColor(0xffd3daeb);
				tableR3.setBackgroundColor(0xffd3daeb);
			} else {
				tableR.setBackgroundColor(Color.WHITE);
				tableR2.setBackgroundColor(Color.WHITE);
				tableR3.setBackgroundColor(Color.WHITE);
			}
			// 정산내역목록 리스트에 뿌려주기 위해 각종 string을 붙여 사용하게끔 추가.
			text_date_and_money_list.setText("\t" + expTime[i] + "\t사용금액: " + MEDataHandler.addComma(expMoney[i]) + "원");
			text_date_and_money_list.setTextColor(Color.BLACK);
			text_date_and_money_list.setTextSize(18);

			text_pjt_list.setText("\t" + "프로젝트 : " + expPjtName[i]);
			text_pjt_list.setTextColor(Color.BLACK);
			text_pjt_list.setTextSize(15);

			text_acnt_list.setText("\t" + "계정 : " + expAcntName[i]);
			text_acnt_list.setTextColor(Color.BLACK);
			text_acnt_list.setTextSize(15);

			// 데이터 주고받기위해 다른문자들이 추가되면 안됨.
			text_date.setText(expTime[i]);
			text_pjt.setText(expPjtName[i]);
			text_acnt.setText(expAcntName[i]);
			text_money.setText(expMoney[i]);
			acntCode = expAcntCode[i];

			tableR.addView(text_date_and_money_list);
			tableR2.addView(text_pjt_list);
			tableR3.addView(text_acnt_list);

			numberTable.addView(tableR, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			numberTable2.addView(tableR2, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			numberTable3.addView(tableR3, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

			tableR2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					new AlertDialog.Builder(MEExpenseListResult.this)
							.setTitle("정산내역 상세정보")
							.setMessage(
									"정산일자 : " + text_date.getText().toString() + "\n" + "프로젝트 : " + text_pjt.getText().toString() + "\n"
											+ "계정 : " + text_acnt.getText().toString() + "\n" + "계정코드 : " + acntCode + "\n" + "사용금액 : "
											+ text_money.getText().toString())
							.setPositiveButton("수정", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									int num = 47;
									Intent I = new Intent(MEExpenseListResult.this, SocketClientService.class);
									I.putExtra("id", num);// 47
									I.putExtra("myId", myId);// 사번
									I.putExtra("date", text_date.getText().toString());// 정산일자
									I.putExtra("money", text_money.getText().toString());// 사용금액
									I.putExtra("pCode", pcd);// 플젝트코드
									I.putExtra("code", acntCode);// 계정코드
									I.putExtra("sDay", fromD);
									I.putExtra("eDay", toD);
									I.putExtra("loginAddr", addr);
									I.putExtra("loginPort", port);
									// I.putExtra("aCode", acd);
									I.putExtra("aCode", acntCode);
									I.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
									startService(I);
									finish();

								}
							}).setNegativeButton("삭제", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog, int which) {
									new AlertDialog.Builder(MEExpenseListResult.this).setTitle("알림").setMessage("정말 삭제 하시겠습니까?")
											.setPositiveButton("예", new DialogInterface.OnClickListener() {
												public void onClick(DialogInterface dialog, int which) {
													int num = 46;
													Intent I = new Intent(MEExpenseListResult.this, SocketClientService.class);
													I.putExtra("id", num);// 46
													I.putExtra("myId", myId);// 사번
													I.putExtra("date", text_date.getText().toString());// 정산일자
													I.putExtra("pCode", pcd);// 플젝트코드
													I.putExtra("code", acntCode);// 계정코드
													I.putExtra("sDay", fromD);
													I.putExtra("eDay", toD);
													I.putExtra("aCode", acd);
													I.putExtra("loginAddr", addr);
													I.putExtra("loginPort", port);
													I.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
													startService(I);
													finish();
												}
											}).setNegativeButton("아니오", null).show();
								}
							}).setNeutralButton("취소", null).show();
				}
			});
		}
	}

	private void getList() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("start", false);
		String param = receiveI.getStringExtra("param");
		if (doYN) {
			myId = receiveI.getStringExtra("myId");
			expTime = receiveI.getStringArrayExtra("expTime"); // 정산일자 :
			expNumber = receiveI.getStringArrayExtra("expNumber");// 플젝코드
			expPjtName = receiveI.getStringArrayExtra("expPname");// 플젝명칭
			expAcntCode = receiveI.getStringArrayExtra("expAcode");// 계정코드
			expAcntName = receiveI.getStringArrayExtra("expAname");// 계정명칭
			expMoney = receiveI.getStringArrayExtra("expMoney");// 결제금액
			expSerial = receiveI.getStringArrayExtra("expSerial");// 정산번호
			fromD = receiveI.getStringExtra("sDay");
			toD = receiveI.getStringExtra("eDay");
			pcd = receiveI.getStringExtra("pjtCode");
			acd = receiveI.getStringExtra("acntCode");// 선택포함
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");

			if ("no".equals(param)) {
			} else if ("delOk".equals(param)) {
				new AlertDialog.Builder(MEExpenseListResult.this).setTitle("알림").setMessage("정산내역 삭제 성공하였습니다.")
						.setPositiveButton("OK", null).show();

			} else if ("upOk".equals(param)) {
				new AlertDialog.Builder(MEExpenseListResult.this).setTitle("알림").setMessage("정산내역 수정 성공하였습니다.")
						.setPositiveButton("OK", null).show();

			}

		}
	}
}
